﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using iPublic;

namespace HRecycle.Comm
{
    /// <summary>
    /// 广告轮播，海宏软件
    ///   参考：https://www.cnblogs.com/feiyuhuo/p/4960357.html
    /// 修改记录：
    ///   20171216，zch，创建基本功能
    /// </summary>
    public partial class FrmGuangGao : Form
    {
        public FrmGuangGao()
        {
            InitializeComponent();
        }

        int nJianGe = 0;
        HtmlDocument htmlDoc = null;
        private void FrmGuangGao_Load(object sender, EventArgs e)
        {
            try
            {
                showInfo("★★" + DateTime.Now.ToString() + "进入广告单元★★");
                if (!iDefine.debugMode) FormBorderStyle = FormBorderStyle.None;
                WindowState = FormWindowState.Maximized;
                //
                lbl_machineID.Text = iDefine.SysConfig.machineID;
                //
                wb_main.Dock = DockStyle.Fill;
                if (nJianGe <= 0) nJianGe = Convert.ToInt16(lbl_time.Text);
            }
            catch (Exception x)
            {
                showInfo(x.Message);
            }
        }

        private void FrmGuangGao_Shown(object sender, EventArgs e)
        {
            try
            {
                tm_main.Tag = 0;
                if (iDefine.guanggao == null) iDefine.guanggao = new iPublic.iDefine.json广告清单();
                if (iDefine.guanggao.data.Count > 0) wb_main.Url = new Uri(iDefine.guanggao.data[0].ad_img);
                //
                tm_main.Enabled = true;
            }
            catch (Exception x)
            {
                showInfo(x.Message);
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tm_main_Tick(object sender, EventArgs e)
        {
            if (iDefine.guanggao == null) iDefine.guanggao = new iPublic.iDefine.json广告清单();
            Timer tm = sender as Timer;
            bool lEnable = tm.Enabled;
            tm.Enabled = false;             //关闭自身
            //
            int n = 0, nNext = Convert.ToInt16(tm.Tag) + 1, nT = Convert.ToInt16(lbl_time.Text) - 1;
            string s = "";
            iDefine.json广告清单.One广告 gg = null;
            try
            {
                if (nT > 0)
                {
                    lbl_time.Text = nT.ToString();
                }
                else
                {   //换下一个广告
                    gg = new iDefine.json广告清单.One广告();
                    if (nNext >= iDefine.guanggao.data.Count) nNext = 0;
                    tm.Tag = nNext;
                    gg = iDefine.guanggao.data[nNext];
                    s = gg.ad_img;
                    if (!string.IsNullOrEmpty(s)) wb_main.Url = new Uri(s);
                    lbl_title.Text = gg.ad_title;
                    //恢复6秒
                    lbl_time.Text = nJianGe.ToString();
                }
            }
            catch (Exception x)
            {
                showInfo(x.Message);
            }
            tm.Enabled = lEnable;
        }

        private void FrmGuangGao_MouseClick(object sender, MouseEventArgs e)
        {
            btn_close_Click(sender, e);
        }
        private void FrmGuangGao_KeyPress(object sender, KeyPressEventArgs e)
        {
            btn_close_Click(sender, e);
        }
        private void FrmGuangGao_KeyDown(object sender, KeyEventArgs e)
        {
            btn_close_Click(sender, e);
        }
        private void wb_main_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            btn_close_Click(sender, e);
        }

        public void showInfo(string sInfo, bool lTime = true)
        {
            lbl_info.Text = sInfo;
            iLog.writeLogFile(sInfo);
        }

        private void wb_main_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = wb_main;
            try
            {
                //webBroswer本身不支持鼠标事件，给他加上
                if (wb_main.Document != null)
                {
                    htmlDoc = wb.Document;
                    htmlDoc.Click += htmlDoc_Click;
                    htmlDoc.MouseDown += htmlDoc_MouseDown;
                    htmlDoc.MouseMove += htmlDoc_MouseMove;
                    htmlDoc.ContextMenuShowing += htmlDoc_ContextMenuShowing;
                    htmlDoc.BackColor = Color.Black;
                    //C:\WINDOWS\system32\mshtml.dll
                    //IHTMLDocument2 vDocument = htmlDoc.DomDocument;
                }
                //将所有的链接的目标，指向本窗体
                foreach (HtmlElement archor in wb.Document.Links)
                {
                    archor.SetAttribute("target", "_self");
                }
                //将所有的FORM的提交目标，指向本窗体
                foreach (HtmlElement form in wb.Document.Forms)
                {
                    form.SetAttribute("target", "_self");
                }
            }
            catch (Exception x)
            { showInfo(x.Message); }
        }
        void htmlDoc_ContextMenuShowing(object sender, HtmlElementEventArgs e)
        {   // stop the right mouse Menu
            //btn_close_Click(sender, e);
            e.ReturnValue = false;
        }
        void htmlDoc_MouseMove(object sender, HtmlElementEventArgs e)
        {   //Console.WriteLine("Mouse Move");
            //btn_close_Click(sender, e);
        }
        void htmlDoc_MouseDown(object sender, HtmlElementEventArgs e)
        {   //Console.WriteLine("Mouse Down");
            btn_close_Click(sender, e);
        }
        void htmlDoc_Click(object sender, HtmlElementEventArgs e)
        {   //Console.WriteLine("Mouse Click");
            // stop mouse events moving on to the HTML doc
            btn_close_Click(sender, e);
            e.ReturnValue = false;
        }

        private void wb_main_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            //e.Cancel = true;
        }

        private void wb_main_FileDownload(object sender, EventArgs e)
        {
            
        }

        private void wb_main_NewWindow(object sender, CancelEventArgs e)
        {   //禁止打开新窗口
            e.Cancel = true;
        }
    }
}
